package com.xintonghua.base.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xintonghua.base.R;


/**
 * 自定义的条目文本
 * Created by hao on 16/8/22.
 */
public class MyItemTextView extends LinearLayout {
    private View rootView, vw_line;
    private TextView tv_title, tv_msg;

    public MyItemTextView(Context context) {
        super(context);
    }

    public MyItemTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.utils_MyItemTextView);

        LayoutInflater inflater = LayoutInflater.from(context);
        rootView = inflater.inflate(R.layout.utils_widget_itemtextview, this);

        //是否含有底边
        boolean bottomLine = a.getBoolean(R.styleable.utils_MyItemTextView_utils_bottomLine, true);
        //标题的字体大小
        float titleSize = a.getDimension(R.styleable.utils_MyItemTextView_utils_titleSize, context.getResources().getDimension(R.dimen.normal));
        //是否有右边的箭头
        boolean next = a.getBoolean(R.styleable.utils_MyItemTextView_utils_next, true);
        int paddingRight = a.getDimensionPixelOffset(R.styleable.utils_MyItemTextView_utils_paddingRight, 0);
        int paddingLeft = a.getDimensionPixelOffset(R.styleable.utils_MyItemTextView_utils_paddingLeft, 0);
        int colorId = a.getColor(R.styleable.utils_MyItemTextView_utils_msgColor, context.getResources().getColor(R.color.black));
        int lineColor = a.getColor(R.styleable.utils_MyItemTextView_utils_lineColor, context.getResources().getColor(R.color.line_grey));
        int titleColor = a.getColor(R.styleable.utils_MyItemTextView_utils_titleColor, context.getResources().getColor(R.color.black));
        float msgSize = a.getDimension(R.styleable.utils_MyItemTextView_utils_msgSize, context.getResources().getDimension(R.dimen.normal));
        Drawable drawableLeft = a.getDrawable(R.styleable.utils_MyItemTextView_utils_drawableLeft);
        int drawPadding = a.getDimensionPixelOffset(R.styleable.utils_MyItemTextView_utils_drawPadding, 0);
        //标题控件
        tv_title = (TextView) rootView.findViewById(R.id.tv_title);
        //副标题控件
        tv_msg = (TextView) rootView.findViewById(R.id.tv_msg);
        //底部分割线
        vw_line = rootView.findViewById(R.id.vw_line);

        hasNext(next);

        setMsgColor(colorId);

        tv_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
        tv_title.setTextColor(titleColor);
        tv_title.setPadding(paddingLeft, 0, 0, 0);
        if (drawableLeft != null) {
            drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
            tv_title.setCompoundDrawablePadding(drawPadding);
            tv_title.setCompoundDrawables(drawableLeft, null, null, null);
        }

        setTitle(a.getString(R.styleable.utils_MyItemTextView_utils_title));
        setMsg(a.getString(R.styleable.utils_MyItemTextView_utils_msg));
        tv_msg.setPadding(0, 0, paddingRight, 0);
        tv_msg.setTextSize(TypedValue.COMPLEX_UNIT_PX, msgSize);


        int left = getPaddingLeft();
        int right = getPaddingRight();
        int top = getPaddingTop();
        int bottom = getPaddingBottom();

        vw_line.setVisibility(bottomLine ? VISIBLE : GONE);
        vw_line.setBackgroundColor(lineColor);

        setPadding(left, top, right, bottom);

        a.recycle();
    }

    public void setIcon(int icon) {
        Drawable drawable = getResources().getDrawable(icon);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tv_title.setCompoundDrawables(drawable, null, null, null);
    }


    public void setLine(boolean b) {
        vw_line.setVisibility(b ? VISIBLE : GONE);
    }

    /**
     * 设置title
     *
     * @param title 标题内容
     */
    public void setTitle(SpannableString title) {
        if (title != null) {
            tv_title.setText(title);
        }
    }

    /**
     * 设置title
     *
     * @param title 标题内容
     */
    public void setTitle(String title) {
        if (title != null) {
            tv_title.setText(title);
        }
    }

    /**
     * 是否显示msg
     *
     * @param visiable 是否显示
     */
    public void visiableMsg(boolean visiable) {
        tv_msg.setVisibility(visiable ? VISIBLE : GONE);
    }

    /**
     * 设置副标题
     *
     * @param msg 副标题内容
     */
    public void setMsg(String msg) {
        if (msg != null) {
            tv_msg.setText(msg);
        }
    }
    /**
     *获取副标题
     */
    public String getMsg() {
            return tv_msg.getText().toString().trim();
    }

    /**
     * 设置右边的按钮
     *
     * @param hasNext
     */
    public void hasNext(boolean hasNext) {
        tv_msg.setCompoundDrawablesWithIntrinsicBounds(0, 0, hasNext ? R.drawable.utils_next : 0, 0);
    }

    /**
     * 设置副标题的颜色
     *
     * @param colorId 颜色值
     */
    public void setMsgColor(int colorId) {
        tv_msg.setTextColor(colorId);
    }
}
